package com.demo.functionPackage;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class PredicateTest {
    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> result = new ArrayList<>();
        for(T s: list) {
            if(p.test(s))
                result.add(s);
        }
        return result;
    }

    public static void main(String[] args) {
        Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
        List<String> listOfStrings = new ArrayList<>();
        listOfStrings.add("");
        listOfStrings.add("");
        listOfStrings.add("1234");
        listOfStrings.add("abcdefg");
        System.out.println("Original String list: " + listOfStrings);
        List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
        System.out.println("NoEmpty String list: " + nonEmpty);
    }
}
